package com.huanghuang.shuadan.controller.user;

import com.huanghuang.shuadan.common.utils.ImgTool.CaptchaTool;
import com.huanghuang.shuadan.common.utils.jwt.JWTTooL;
import com.huanghuang.shuadan.manager.user.converter.UserConverter;
import com.huanghuang.shuadan.service.entity.base.PageReqDTO;
import com.huanghuang.shuadan.service.entity.base.Result;
import com.huanghuang.shuadan.service.entity.req.UserRegisterReqDTO;
import com.huanghuang.shuadan.service.entity.req.UsernameReqDTO;
import com.huanghuang.shuadan.service.entity.res.UserResDTO;
import com.huanghuang.shuadan.service.user.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Map;

/**
 * @Author suxiaoxiao
 * @Date 2019/7/13 23:45
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/user")
@Api(tags = "用户信息", value = "用户信息")
public class UserController {

    /**
     * 用户服务类
     */
    @Autowired
    private UserService userService;

    @Autowired
    private HttpSession session;

    @Autowired
    private JWTTooL jwtDemo;

    @RequestMapping(value = "/queryAllUser", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有用户信息", notes = "查询所有用户信息")
    public Result<UserResDTO> queryAllUser(@ApiParam(name="通用入参",value="传入json格式",required=true)
                                           @RequestBody PageReqDTO pageReqDTO) {
        return userService.queryAllUser(pageReqDTO);
    }

    @RequestMapping(value = "/jwt", method = RequestMethod.POST)
    @ApiOperation(value = "测试生成jwt", notes = "测试生成jwt")
    public Result<UserResDTO> jwt(@ApiParam(name="用户名",value="传入json格式",required=true)
                                      @RequestBody UsernameReqDTO usernameReqDTO) {
        JWTTooL.verifyToken(jwtDemo.createTokenWithClaim(userService.selectUserByUsername(usernameReqDTO).getResult()));

        return  userService.selectUserByUsername(usernameReqDTO);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ApiOperation(value = "用户注册", notes = "用户注册")
    public Result<Boolean> register(@ApiParam(name="用户名",value="传入json格式",required=true)
                                       @RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        return  userService.registerUser(userRegisterReqDTO);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "用户登陆", notes = "用户登陆")
    public Result<UserResDTO> login(@ApiParam(name="用户名，密码",value="传入json格式",required=true)
                                    @RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        return userService.login(userRegisterReqDTO);
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ApiOperation(value = "当前用户信息", notes = "当前用户信息")
    public Result<UserResDTO> info() {
        return userService.info();
    }

    @RequestMapping(value = "/loginOut", method = RequestMethod.GET)
    @ApiOperation(value = "当前用户退出", notes = "用户退出")
    public Result<Boolean> loginOut() {
        session.removeAttribute("user");
        session.removeAttribute("captcha");
        return new Result(true);
    }

    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    @ApiOperation(value = "用户登陆验证码", notes = "用户登陆验证码")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
//        HttpSession session = request.getSession();

        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);
        response.setContentType("image/jpeg");

        Map<String,Object> codeMap = CaptchaTool.generateCodeAndPic();

        session.setAttribute("captcha", codeMap.get("captcha").toString());

        // 将图像输出到Servlet输出流中。
        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            ImageIO.write((RenderedImage) codeMap.get("captchaPic"), "jpeg", sos);
            sos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
